/**
 * 
 */
package handwritten.grid;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import utility.math.Vector2D;

/**
 * @author tioz
 *
 */
public class BasicInputGrid extends InputGrid    {
	//separation beteween the cell
	
	final static BasicStroke dashed =
	        new BasicStroke(1.0f,
	                        BasicStroke.CAP_BUTT,
	                        BasicStroke.JOIN_MITER,
	                        10.0f, new float[]{2}, 0.0f);
	final static BasicStroke nodashed =
	        new BasicStroke(1.0f);
	
	int lineWidth;
	
	int startX,startY,endX,endY;
	
	//Total width and eight (cell + linee)
	int cellWidth;
	int cellHeight;
	//number of cell along x
	int numberOfCellX;
	//center the cell with an offset
	int offsetX;
	
	//number of cell along y
	int numberOfCellY;
	//center the cell with an offset
	int offsetY;
	
	int beginOfgridX;
	int endOfgridX;
	
	int beginOfgridY;
	int endOfgridY;
	
	public BasicInputGrid(int lineWidth,int textCellWidth, int textCellHeight, int textUnderlineHeight) {
		super(textCellWidth,textCellHeight,textUnderlineHeight);
				
		startX=0;
		startY=0;
		endX=0;
		endY=0;
		cellWidth=0;
		cellHeight=0;
		numberOfCellX=0;
		offsetX=0;
		numberOfCellY=0;
		offsetY=0;
		beginOfgridX=0;
		endOfgridX=0;
		beginOfgridY=0;
		endOfgridY=0;
		this.lineWidth = lineWidth;
	}
	
	/* (non-Javadoc)
	 * @see textRecognition.gesture.InputGrid#update(int, int)
	 */
	@Override
	public void update(int startX,int startY,int endX,int endY) {
		this.startX = startX;
		this.startY = startY;
		this.endX = endX;
		this.endY = endY;
		
		//Total width and eight (cell + line== 1/2 line on each side)
		cellWidth = textCellWidth  +lineWidth;
		cellHeight= textCellHeight +lineWidth;
		//number of cell along x (we have an extra line to draw)
		numberOfCellX =(endX-startX-lineWidth)/(cellWidth);
		//center the cell with an offset
		offsetX       =(((endX-startX)%cellWidth)+lineWidth)/2;
		
		//number of cell along y
		numberOfCellY =(endY-startY-lineWidth)/cellHeight;
		//center the cell with an offset
		offsetY       =(((endY-startY)%cellHeight)+lineWidth)/2;
		
    	beginOfgridX = startX+offsetX;
    	endOfgridX   = startX+offsetX + numberOfCellX*cellWidth ;
    	
    	beginOfgridY = startY+offsetY;
    	endOfgridY   = startY+offsetY + numberOfCellY*cellHeight ;
		
	}
	
	public void paint(Graphics2D g){
				
		g.setStroke(new BasicStroke(lineWidth));
		g.setColor(new Color(0,0,0));
		//there is numberOfcell +1 lines
		for(int i=0; i<= numberOfCellX;i++){ 
			
			int xHeight= beginOfgridX + i*cellWidth;
											
			Line2D.Float line = new Line2D.Float(xHeight,beginOfgridY,xHeight,endOfgridY);
	    	g.draw(line);
    	}
		for(int i=0; i<= numberOfCellY;i++){ 
							
			int yWidth= beginOfgridY + i*cellHeight;
			
			Line2D.Float line1 = new Line2D.Float(beginOfgridX,yWidth,endOfgridX,yWidth);
			g.draw(line1);
			if(i!=0)
			{
				Line2D.Float line2 = new Line2D.Float(beginOfgridX,yWidth-textUnderlineHeight,endOfgridX,yWidth-textUnderlineHeight);
				g.setStroke(dashed);
				g.draw(line2);
			}
			g.setStroke(nodashed);
    	}
		
		//check that getBoundary work
		//g.setColor(new Color(200,0,60));
		//g.draw(new Line2D.Float(320,300,320,300));
		//Vector2D[] corner =getCellBoundary(new Vector2D(320,300));
		//g.draw(new Line2D.Float(corner[0].getX(),corner[0].getY(),corner[0].getX(),corner[0].getY()));
		//g.draw(new Line2D.Float(corner[1].getX(),corner[1].getY(),corner[1].getX(),corner[1].getY()));
		
	}
	
	



	
	/**
	 * retourn the real boundary of the Cell in which pos is inside
	 * @param pos
	 * @return
	 */
	
	public int[] getCellNumber(Vector2D pos)
	{
		int nX  = (int)(pos.getX()-beginOfgridX)/(textCellWidth + lineWidth);
		int nY  = (int)(pos.getY()-beginOfgridY)/(textCellHeight + lineWidth);
		
		//System.out.println(nX+" and "+nY);
		int tmp[]={nX,nY};
		
		return tmp;
	}
	
	
	public Vector2D[] getCellBoundary(Vector2D pos)
	{
		int nX  = (int)(pos.getX()-beginOfgridX)/(textCellWidth + lineWidth);
		int nY  = (int)(pos.getY()-beginOfgridY)/(textCellHeight + lineWidth);
		
		//System.out.println(nX+" and "+nY);
		
		return getCellBoundary(nX,nY);
	}
	
	/**
	 * retourn the real boundary of the {(nx+1),(ny+1)} Cell(not the line)
	 * @param nX
	 * @param nY
	 * @return
	 */
	public Vector2D[] getCellBoundary(int nX,int nY)
	{
		Vector2D top = new Vector2D(beginOfgridX+nX*cellWidth+lineWidth/2
				   				   ,beginOfgridY+nY*cellHeight+lineWidth/2);
		
		Vector2D bottom= new Vector2D( top.getX()+textCellWidth, top.getY()+textCellHeight);
		
		Vector2D tmp[]={top,bottom};
		
		return tmp;
	}
	
	public Vector2D getCellTopLeft(Vector2D pos)
	{
		int nX  = (int)(pos.getX()-beginOfgridX)/(textCellWidth + lineWidth);
		int nY  = (int)(pos.getY()-beginOfgridY)/(textCellHeight + lineWidth);
		
		return  new Vector2D(beginOfgridX+nX*cellWidth+lineWidth/2
							,beginOfgridY+nY*cellHeight+lineWidth/2);		
	}
	
	
	public int getLineWidth() {
		return lineWidth;
	}

	public int getStartX() {
		return startX;
	}

	public int getStartY() {
		return startY;
	}

	public int getEndX() {
		return endX;
	}

	public int getEndY() {
		return endY;
	}

	public int getCellWidth() {
		return cellWidth;
	}

	public int getCellHeight() {
		return cellHeight;
	}

	public int getNumberOfCellX() {
		return numberOfCellX;
	}

	public int getOffsetX() {
		return offsetX;
	}

	public int getNumberOfCellY() {
		return numberOfCellY;
	}

	public int getOffsetY() {
		return offsetY;
	}

	public int getBeginOfgridX() {
		return beginOfgridX;
	}

	public int getEndOfgridX() {
		return endOfgridX;
	}

	public int getBeginOfgridY() {
		return beginOfgridY;
	}

	public int getEndOfgridY() {
		return endOfgridY;
	}
	
}
